/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package info.bondtnt.labs.model.research.impl;

import info.bondtnt.labs.model.research.BoundedDoubleParameter;
import info.bondtnt.labs.model.research.ResearchPackage;

import org.eclipse.emf.common.notify.Notification;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EDataTypeUniqueEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Bounded Double Parameter</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link info.bondtnt.labs.model.research.impl.BoundedDoubleParameterImpl#getFirstValue <em>First Value</em>}</li>
 *   <li>{@link info.bondtnt.labs.model.research.impl.BoundedDoubleParameterImpl#getLastValue <em>Last Value</em>}</li>
 *   <li>{@link info.bondtnt.labs.model.research.impl.BoundedDoubleParameterImpl#getAllValues <em>All Values</em>}</li>
 *   <li>{@link info.bondtnt.labs.model.research.impl.BoundedDoubleParameterImpl#getStepValue <em>Step Value</em>}</li>
 *   <li>{@link info.bondtnt.labs.model.research.impl.BoundedDoubleParameterImpl#getName <em>Name</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class BoundedDoubleParameterImpl extends EObjectImpl implements BoundedDoubleParameter {
	private static final int PRECISION_ENCHASER = 1000;

	/**
	 * The default value of the '{@link #getFirstValue() <em>First Value</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFirstValue()
	 * @generated
	 * @ordered
	 */
	protected static final Double FIRST_VALUE_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getFirstValue() <em>First Value</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFirstValue()
	 * @generated
	 * @ordered
	 */
	protected Double firstValue = FIRST_VALUE_EDEFAULT;

	/**
	 * The default value of the '{@link #getLastValue() <em>Last Value</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLastValue()
	 * @generated
	 * @ordered
	 */
	protected static final Double LAST_VALUE_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getLastValue() <em>Last Value</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLastValue()
	 * @generated
	 * @ordered
	 */
	protected Double lastValue = LAST_VALUE_EDEFAULT;

	/**
	 * The cached value of the '{@link #getAllValues() <em>All Values</em>}' attribute list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getAllValues()
	 * @generated
	 * @ordered
	 */
	protected EList<Double> allValues;

	/**
	 * The default value of the '{@link #getStepValue() <em>Step Value</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStepValue()
	 * @generated
	 * @ordered
	 */
	protected static final Double STEP_VALUE_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getStepValue() <em>Step Value</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStepValue()
	 * @generated
	 * @ordered
	 */
	protected Double stepValue = STEP_VALUE_EDEFAULT;
	
	
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	private Integer countOfValues = 0;

	private String name;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected BoundedDoubleParameterImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ResearchPackage.Literals.BOUNDED_DOUBLE_PARAMETER;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Integer countOfValues() {
		return countOfValues;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Double getFirstValue() {
		return firstValue;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Double getLastValue() {
		return lastValue;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Double> getAllValues() {
		if (allValues == null) {
			allValues = new EDataTypeUniqueEList<Double>(Double.class, this, ResearchPackage.BOUNDED_DOUBLE_PARAMETER__ALL_VALUES);
		}
		return allValues;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Double getStepValue() {
		return stepValue;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Double getValueByIndex(Integer index) {
		Double result = null;
		if (index < this.countOfValues()) {
			Double value = this.getFirstValue();
			final Double stepVal = this.getStepValue();
			value = value*PRECISION_ENCHASER + stepVal*PRECISION_ENCHASER*index;
			result = value/PRECISION_ENCHASER;
		} else {
			throw new IllegalArgumentException("Parameter 'index' must be less than countOfvalues()!");
		}
		
		return result;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setBoundaries(double firstValue, double lastValue, double stepValue) {
		if (firstValue > lastValue) {
			throw new IllegalArgumentException("'lastValue' can't be greater than 'firstValue'");
		}
		
		this.firstValue = firstValue;
		this.lastValue = lastValue;
		this.stepValue = stepValue;
		
		countOfValues = 0;
		Double value = firstValue;
		
		EList<Double> allValuesList = this.getAllValues();
		do {
			allValuesList.add(value);
			countOfValues++;
			value = value + stepValue;
		} while (value.doubleValue() <= lastValue); 
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__FIRST_VALUE:
				return getFirstValue();
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__LAST_VALUE:
				return getLastValue();
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__ALL_VALUES:
				return getAllValues();
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__STEP_VALUE:
				return getStepValue();
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__NAME:
				return getName();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__NAME:
				setName((String)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__NAME:
				setName(NAME_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__FIRST_VALUE:
				return FIRST_VALUE_EDEFAULT == null ? firstValue != null : !FIRST_VALUE_EDEFAULT.equals(firstValue);
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__LAST_VALUE:
				return LAST_VALUE_EDEFAULT == null ? lastValue != null : !LAST_VALUE_EDEFAULT.equals(lastValue);
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__ALL_VALUES:
				return allValues != null && !allValues.isEmpty();
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__STEP_VALUE:
				return STEP_VALUE_EDEFAULT == null ? stepValue != null : !STEP_VALUE_EDEFAULT.equals(stepValue);
			case ResearchPackage.BOUNDED_DOUBLE_PARAMETER__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (firstValue: ");
		result.append(firstValue);
		result.append(", lastValue: ");
		result.append(lastValue);
		result.append(", allValues: ");
		result.append(allValues);
		result.append(", stepValue: ");
		result.append(stepValue);
		result.append(", name: ");
		result.append(name);
		result.append(')');
		return result.toString();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ResearchPackage.BOUNDED_DOUBLE_PARAMETER__NAME, oldName, name));
	}

} //BoundedDoubleParameterImpl
